package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.*
import com.songcha.library_common.R
import com.songcha.library_common.ui.view.listener.IOnBottomNavigationItemSelectListener
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.px2sp
import com.songcha.library_common.util.sp2px

class BottomNavigationItemView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0,
    listener: ((LinearLayout,Boolean,View?)->View?)? = null
) : LinearLayout(context, attrs, defStyle, defRes) , IOnBottomNavigationItemSelectListener {
    private lateinit var mIVIcon:ImageView
    private lateinit var mTvTitle:TextView

    private var mIconSize=28f.dp2px()
    private var mIconNormal=0
    private var mIconSelected=0
    private var mTitle=""
    private var mTextSizeNormal=14f.sp2px()
    private var mTextSizeSelected=16f.sp2px()
    private var mTitleColorNormal=Color.BLACK
    private var mTitleColorSelected=Color.BLACK
    private var mTitleMarginTop=4f.dp2px()
    var TAG=""
    private var mCustomViewListener:((LinearLayout,Boolean,View?)->View?)? = null


    init {
        mCustomViewListener = listener
        initView(context, attrs)
    }

    /* constructor(context: Context,listener:((LinearLayout,Boolean,View?)->View?)? = null) : this(context,null,0,0,listener) {
         mCustomViewListener = listener
     }*/

    private fun initView(context: Context, attrs: AttributeSet?) {
        setWillNotDraw(false)

        if (attrs != null) {
            val ta = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationItemView)
            mIconSize=ta.getDimension(R.styleable.BottomNavigationItemView_bottom_navigation_item_icon_size,28f.dp2px())
            mIconNormal=ta.getResourceId(R.styleable.BottomNavigationItemView_bottom_navigation_item_icon_normal,0)
            mIconSelected=ta.getResourceId(R.styleable.BottomNavigationItemView_bottom_navigation_item_icon_selected,0)
            mTitle=ta.getString(R.styleable.BottomNavigationItemView_bottom_navigation_item_title)?:""
            mTextSizeNormal=ta.getDimension(R.styleable.BottomNavigationItemView_bottom_navigation_item_title_textSize_normal,14f.sp2px())
            mTextSizeSelected=ta.getDimension(R.styleable.BottomNavigationItemView_bottom_navigation_item_title_textSize_selected,16f.sp2px())
            mTitleColorNormal=ta.getColor(R.styleable.BottomNavigationItemView_bottom_navigation_item_title_color_normal,Color.BLACK)
            mTitleColorSelected=ta.getColor(R.styleable.BottomNavigationItemView_bottom_navigation_item_title_color_selected,Color.BLACK)
            mTitleMarginTop=ta.getDimension(R.styleable.BottomNavigationItemView_bottom_navigation_item_title_marginTop,4f.dp2px())
            ta.recycle()
        }

        orientation=VERTICAL
        gravity=Gravity.CENTER

        if(mCustomViewListener != null){
            addView(mCustomViewListener!!(this,false,null))
        }else{
            addIconTitle()
        }

    }

    fun setTitleMarginTop(marginTop:Float){
        mTitleMarginTop = marginTop
        addIconTitle()
    }

    fun addIconTitle(){
        mIVIcon=ImageView(context)
        mTvTitle=TextView(context)
        mIVIcon.layoutParams=LayoutParams(mIconSize.toInt(),mIconSize.toInt())
        mTvTitle.layoutParams=LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        mIVIcon.setImageResource(mIconNormal)

        mTvTitle.text=mTitle
        mTvTitle.textSize=mTextSizeNormal.px2sp()
        mTvTitle.setTextColor(mTitleColorNormal)
        (mTvTitle.layoutParams as LayoutParams).topMargin=mTitleMarginTop.toInt()

        removeAllViews()

        addView(mIVIcon)
        addView(mTvTitle)
    }

    fun setTitle(title:String,textSizeNormal:Float,textColorNormal:Int,textSizeSelected:Float,textColorSelected:Int){
        mTitle=title
        mTextSizeNormal=textSizeNormal
        mTextSizeSelected=textSizeSelected
        mTitleColorNormal=textColorNormal
        mTitleColorSelected=textColorSelected
        addIconTitle()
    }

    fun setIcon(iconSize:Float,iconResIdNormal:Int,iconResIdSelected:Int){
        mIconSize=iconSize
        mIconNormal=iconResIdNormal
        mIconSelected=iconResIdSelected
        addIconTitle()
    }

    override fun onSelect() {
        if(mCustomViewListener != null){
            mCustomViewListener!!(this,true,getChildAt(0))
        }else{
            mIVIcon.setImageResource(mIconSelected)
            mTvTitle.setTextColor(mTitleColorSelected)
            mTvTitle.textSize=mTextSizeSelected.px2sp()
            mTvTitle.typeface= Typeface.defaultFromStyle(Typeface.BOLD)
        }
    }

    override fun onUnSelect() {
        if(mCustomViewListener != null){
            mCustomViewListener!!(this,false,getChildAt(0))
        }else{
            mIVIcon.setImageResource(mIconNormal)
            mTvTitle.setTextColor(mTitleColorNormal)
            mTvTitle.textSize=mTextSizeNormal.px2sp()
            mTvTitle.typeface=Typeface.defaultFromStyle(Typeface.NORMAL)
        }
    }

    /*fun setCustomViewListener(listener:(LinearLayout,Boolean)->Unit) {
        mCustomViewListener = listener
    }*/

}